package util.android.widgets;

import java.util.ArrayList;
import java.util.List;

import com.androidutil.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;

public class TableLayoutCustom 
extends TableLayout
{
	private final List<Float> pesos = new ArrayList<Float>();

	public TableLayoutCustom(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TableLayoutCustom, 0, 0);
	    try {
	        String s = ta.getString(R.styleable.TableLayoutCustom_pesos);
	        if(s!=null) {
	        	String ps[] = s.split(",");	        	
	        	for(String peso : ps) {
	        		if(peso.trim().isEmpty()) {
	        			pesos.add(1f);
	        		}
	        		else {
	        			float p = Float.valueOf(peso);
	        			pesos.add(p);
	        		}
	        	}
	        }
	    } finally {
	        ta.recycle();
	    }
	    //System.out.println("PESOS="+pesos);
	}

	@Override
	public void addView(View child, android.view.ViewGroup.LayoutParams params) {
		super.addView(child, params);
				
		ViewGroup tr = (ViewGroup)child;//Table row
		//AndroidUtil.mostrarToast(getContext(), tr.getChildCount()+"");
		for (int i = 0; i < tr.getChildCount(); i++) {
			View v = tr.getChildAt(i);			
			LinearLayout.LayoutParams ps = (LinearLayout.LayoutParams) v.getLayoutParams();
			
			if(i<pesos.size()) {
				ps.weight = pesos.get(i);
			}
			else {
				ps.weight = 1;
			}				
			//if(i==1 || i==2)
			//	ps.weight = 0.5f;
			//ps.width = 0;
		}
	}
	
	

	
}
